package view;

import javax.swing.*;

import util.Coord;

import controller.Controller;
import controller.DrawingClockListener;
import java.awt.Color;
import model.ModelReader;

public class Stage extends JFrame implements DrawingClockListener{
	private static final long serialVersionUID = 1L;
	private GamePanel[] panels;
	private ModelReader mr;
	private Controller controller;
	private int FPS;
	private long lastTime;

	public Stage(ModelReader world, Controller controller){
		panels = new GamePanel[3];
		this.controller = controller;
		this.mr = world;
		init();
	}

	public ModelReader getModelReader(){
		return mr;
	}

	public Coord getMapCenter(){
		if(panels[2] instanceof WorldView){
			return ((WorldView)panels[2]).getMapCenter();
		}else
			return new Coord(0,0);
	}

	private void init() {
		FPS = 0;
		lastTime = System.currentTimeMillis();
		setSize(1280, 720);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);
		setFocusable(false);

		panels[2] = new WorldView(this, controller, (int)(getWidth()*0.8 - 10), (int)(getHeight()*0.7 - 10));
		panels[1] = new SideBar(this, controller, (int)(getWidth()*0.2 - 10), getHeight() - 35);
		panels[0] = new BottomBar(this, controller, (int)(getWidth()*0.8 - 10), (int)(getHeight()*0.3 - 30));

		panels[2].setLocation((int)(getWidth()*0.2), 5);
		panels[1].setLocation(5, 5);
		panels[0].setLocation((int)(getWidth()*0.2), (int)(getHeight()*0.7));

		for(GamePanel gp : panels){
			add(gp);
		}
	}

	@Override
	public void draw() {
		for(GamePanel gp : panels){
			gp.draw();
		}
		FPS++;
		if(System.currentTimeMillis() - lastTime > 1000){
			lastTime = System.currentTimeMillis();
			setTitle("FPS: " + FPS + "; Number of loaded Chunks: " + mr.numLoadedChunks());
			FPS = 0;
		}
	}
}